<template>
  <div class="container">
    <nav-bar></nav-bar>
    <div class="item-box">
      <div class="search-box">
        <div class="left-box">
          <pub-area
            :maxSelectLevel="3"
            @change="changeArea"
            customRightIcon
            label=""
            :isLink="false"
            valueIsLast
          ></pub-area>
        </div>
      </div>
      <div class="content-box">
        <div class="top-total">
          <div class="left-total">
            <img src="../../assets/img/view/lhsqs.png" alt="" />
            <div class="value-box">
              <div class="text-ch">轮候申请数（条）</div>
              <div class="text-num">
                <count-to
                  :startVal="0"
                  :endVal="Number(state.info.total)"
                  :decimals="0"
                  :duration="2000"
                ></count-to>
              </div>
            </div>
          </div>
          <div class="right-total">
            <img src="../../assets/img/view/lhhtgs.png" alt="" />
            <div class="value-box">
              <div class="text-ch">轮候核通过数（条）</div>
              <div class="text-num">
                <count-to
                  :startVal="0"
                  :endVal="Number(state.info.passNum)"
                  :decimals="0"
                  :duration="2000"
                ></count-to>
              </div>
            </div>
          </div>
        </div>
        <div class="chart-box" ref="chart"></div>
      </div>
    </div>
    <div class="item-box">
      <div class="title-box">
        <div class="left-title">轮候申请趋势</div>
        <div class="right-title">近6个月数据</div>
      </div>
      <div class="chart-box2" ref="chart1"></div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onBeforeMount, onMounted } from 'vue'
import { getJGLHCWSYL } from '@/api/xcc'
import router from '@/router/index'
import { useUserStore } from '@/pinia/modules/user'
import { useRouter } from 'vue-router'
import { CountTo } from 'vue3-count-to'
import * as echarts from 'echarts'

var option
const chart1 = ref()
const chart = ref()
const state = reactive({
  info: {
    
  },
  form: {
    areaCode: useUserStore().userLastArea
  }
})

const initChart = (data) => {
  let chartDom = chart.value
  var myChart = echarts.init(chartDom)
  option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      bottom: '0%',
      left: 'center'
    },
    series: [
      {
        name: '机构轮候床位',
        type: 'pie',
        radius: ['30%', '50%'],
        // center:['50%','40%'],
        avoidLabelOverlap: false,
        label: {
          show: true,
          formatter: '{d}%',
          fontSize: 15
        },
        itemStyle: {
          borderRadius: 0,
          borderColor: '#fff',
          borderWidth: 5
        },
        data: [
          { value: data.passNum, name: '核通过数' },
          { value: data.notPassNum, name: '审核不通过或未审核数' }
        ],
        emphasis: {
          scale: true, //是否开启高亮后扇区的放大效果。
          scaleSize: 5 //高亮后扇区的放大尺寸，在开启 emphasis.scale 后有效。
        }
      }
    ],
    backgroundColor: '#fff',
    color: ['#2E65FD', '#FEC02A']
  }

  option && myChart.setOption(option)
  myChart.dispatchAction({
    type: 'highlight',
    name: '核通过数' //可以通过name指定，或者数据项的 index
  })
}

const initChart1 = (data) => {
  let chartDom = chart1.value
  var myChart = echarts.init(chartDom)
  let month = []
  let value = []
  data.trend.forEach(element => {
    month.push(element.code)
    value.push(element.num)
  });
  option = {
    color: ['#4375E4'],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        label: {
          backgroundColor: '#6a7985'
        }
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '10%',
      top: '3%',
      containLabel: true
    },
    xAxis: [
      {
        type: 'category',
        boundaryGap: false,
        data: month
      }
    ],
    yAxis: [
      {
        type: 'value',
        splitLine: {
          //   lineStyle: {
          //     // 设置背景横线
          //     color: 'red'
          //   }
        }
      }
    ],
    series: [
      {
        name: '轮候申请趋势',
        type: 'line',
        stack: 'Total',
        smooth: true,
        lineStyle: {
          width: 0
        },
        showSymbol: false,
        areaStyle: {
          opacity: 0.8,
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: '#4375E4'
            },
            {
              offset: 1,
              color: 'rgba(67, 117, 228, 0)'
            }
          ])
        },
        emphasis: {
          focus: 'series'
        },
        data: value
      }
    ]
  }

  option && myChart.setOption(option)
}

const changeArea = val => {
  state.form.areaCode = val
  getData()
}

const getData = async () => {
  let res = await getJGLHCWSYL(state.form)
  if (res.succeed) {
    state.info = res.data
    initChart(res.data)
    initChart1(res.data)
  }
}

onMounted(() => {
  getData()
})
onBeforeMount(() => {})
</script>
<style lang="less" scoped>
.container {
  background-color: #f8f8f8;
  padding-top: 20px;
  .item-box {
    background-color: #fff;
    .search-box {
      display: flex;
      height: 90px;
      width: 100%;
      border-bottom: 1px solid #e6e6e6;
      .left-box {
        height: 100%;
        :deep(.van-cell) {
          width: 220px;
          height: 100%;
          input {
            // max-width: 400px;
          }
        }
      }
    }
    .content-box {
      padding: 30px 40px;
      .top-total {
        display: flex;
        position: relative;
        .left-total {
          width: 100%;
          position: relative;
          margin-right: 15px;
          display: flex;
          img {
            width: 100%;
          }
          .value-box {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            width: 100%;
            .text-ch {
              padding-left: 60px;
              width: calc(100% - 60px);
              font-size: 24px;
              font-weight: 400;
              color: #3852a9;
            }
            .text-num {
              padding-left: 60px;
              width: calc(100% - 60px);
              font-size: 40px;
              font-weight: normal;
              color: #3852a9;
            }
          }
        }
        .right-total {
          width: 100%;
          margin-left: 15px;
          position: relative;
          display: flex;
          img {
            width: 100%;
          }
          .value-box {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            width: 100%;
            .text-ch {
              padding-left: 60px;
              width: calc(100% - 60px);
              font-size: 24px;
              font-weight: 400;
              color: #219421;
            }
            .text-num {
              padding-left: 60px;
              width: calc(100% - 60px);
              font-size: 40px;
              font-weight: normal;
              color: #219421;
            }
          }
        }
      }
      .chart-box {
        height: 450px;
      }
    }
    .title-box {
      display: flex;
      justify-content: space-between;
      margin-top: 20px;
      padding: 30px 40px;
      border-bottom: 1px solid #E6E6E6;
      margin-bottom: 40px;
      .left-title {
        font-size: 30px;
        font-weight: bold;
        color: #000000;
      }
      .right-title {
        font-size: 24px;
        font-weight: 500;
        color: #999999;
      }
    }
    .chart-box2 {
      height: 450px;
    }
  }
}
</style>
